import * as AMapLoader from "@amap/amap-jsapi-loader";
import { shallowRef } from "vue";

// window._AMapSecurityConfig = {
//   securityJsCode: "「您申请的安全密钥」",
// };

/**
 * 初始化amap 在父组建的onMounted执行结束后才会进行赋值操作
 * @param nodeId dom元素的id用来设置地图容器id
 * @param aMapOptions 初始化地图配置项
 * @param callback 在初始化地图后的回调函数
 * @returns 数组 0 new 之后的map对象 1 AMap构造函数 2 地图中所用到的标记icon
 */
export default function useAMap(
  nodeId: string,
  aMapOptions: Record<string, any> & { aMapType?: string },
  callback?: () => void
) {
  const map = shallowRef();
  const amap = shallowRef();
  AMapLoader.load({
    key: "69826d8b560eb047de101d7fae9ff485", // 申请好的Web端开发者Key，首次调用 load 时必填
    version: "2.0", // 指定要加载的 JS API 的版本，缺省时默认为 1.4.15
    plugins: [
      "AMap.Polygon",
      "AMap.Scale",
      "AMap.HawkEye",
      "AMap.ToolBar",
      "AMap.ControlBar",
      "AMap.PlaceSearch",
      "AMap.DistrictSearch",
      "AMap.HeatMap",
      "AMap.3DTilesLayer",
      "AMap.IndoorMap",
      "AMap.MoveAnimation",
      "AMap.ElasticMarker",
      "AMap.MarkerCluster",
      "AMap.IndexCluster",
      "AMap.MouseTool",
      "AMap.BezierCurveEditor",
      "AMap.RectangleEditor",
      "AMap.CircleEditor",
      "AMap.EllipseEditor",
      "AMap.GeoJSON",
      "AMap.PolylineEditor",
      "AMap.PolygonEditor",
      "AMap.AutoComplete",
      "AMap.Driving",
      "AMap.Walking",
      "AMap.Riding",
      "AMap.Transfer",
      "AMap.Geocoder",
      "AMap.GraspRoad",
      "AMap.StationSearch",
      "AMap.LineSearch",
      "AMap.ArrivalRange",
      "AMap.CitySearch",
      "AMap.Geolocation",
      "AMap.Weather",
      "AMap.RangingTool",
    ], // 需要使用的的插件列表，如比例尺'AMap.Scale'等
  })
    .then((AMap) => {
      map.value = new AMap.Map(nodeId, {
        ...aMapOptions,
      });
      amap.value = AMap;
      callback?.();
    })
    .catch((e: any) => {
      console.log(e);
    });

  return [map, amap];
}
